<template>
  <div class="wrapper">
    <!-- 顶部导航栏 -->
    <nav-bar class="animate__fadeInDown animate__animated" />
    <!-- 顶部导航栏 -->
    <!-- 侧边导航栏 -->
    <van-sidebar v-model="activeKey" class="sidebar">
      <van-sidebar-item v-for="item in categoryList" :key="item.id">
        <template #title>
          <div class="title-container" @click="onClickCategory(item.id)">
            <div class="title">{{ item.name }}</div>
            <div class="price">￥{{ item.money }}起</div>
          </div>
        </template>
      </van-sidebar-item>
    </van-sidebar>
    <!-- 侧边导航栏 -->
    <!-- 右侧商品展示栏 -->
    <div class="showGoods">
      <car-list-goods-box
        v-for="(item, index) in goodsList"
        :key="index"
        :data="item"
      />
    </div>
    <!-- 右侧商品展示栏 -->
  </div>
</template>

<script>
import NavBar from "./components/NavBar.vue";
import CarListGoodsBox from "./components/CarListGoodsBox.vue";
import { getCategoryList, getCategoryGoods } from "../../api/category";

export default {
  components: { NavBar, CarListGoodsBox },
  data() {
    return {
      activeKey: 0,
      // 分类列表数据
      categoryList: null,
      // 当前分类商品数据列表
      goodsList: null,
    };
  },
  methods: {
    // 获取分类数据列表
    async localCategoryList() {
      try {
        const res = await getCategoryList();
        this.categoryList = res.data;
      } catch (error) {
        console.log(error);
      }
    },
    // 点击分类触发，比如点击热销，经济型..触发
    // 参数为当前分类ID，用来请求对应的数据
    onClickCategory(id) {
      this.$toast.loading("加载中");
      getCategoryGoods(id)
        .then((res) => {
          this.goodsList = res.data;
          this.$toast.clear()
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 初始化
    initGoodsList(id) {
      this.$toast.loading("加载中");
      getCategoryGoods(id)
        .then((res) => {
          this.goodsList = res.data;
          this.$toast.clear()
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
  created() {
    this.localCategoryList();
    this.initGoodsList(1)
  },
};
</script>

<style lang="scss" scoped>
.wrapper {
  position: relative;
  .sidebar {
    width: 70px;
    height: 621px;
    .van-sidebar-item {
      padding: 10px;
    }
    .van-sidebar-item--select::before {
      width: 3px;
      height: 40px;
      background: #f19066;
    }
    .title-container {
      display: flex;
      flex-direction: column;
      align-items: center;
      font-size: 10px;
      .price {
        color: #333;
        font-weight: 500;
      }
    }
  }
  .showGoods {
    position: absolute;
    top: 46px;
    left: 70px;
    width: 305px;
    height: 621px;
    background: #f3f3f3;
    overflow-x: hidden;
    overflow-y: scroll;
  }
}
</style>